<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="css/style.css"/>
    <script type="text/javascript" src="js/bootstrap.js"></script>
    <script type="text/javascript" src="js/ckform.js"></script>
    <script type="text/javascript" src="js/common.js"></script>
    <script type="text/javascript" src="js/showdate.js"></script>
    <script src="js/jquery.min.js?v=2.1.4"></script>
    <style type="text/css">
        body {
            font-size: 20px;
            padding-bottom: 40px;
            background-color: #e9e7ef;
            font-size: 17px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }
    </style>
    <%--提示消息插件--%>
    <script src="js/Dream-Msg/lib/dream-msg.min.js"></script>
    <script>
        //获取bid
        let bid = ${param.bid}
            $(function () {
                $("#bFk").val(bid)
                //一加载该页面立即查询博客详情
                getBlogDetail(bid);
            })

        //删除评论2
        $(document).on("click", ".deletecomment", function () {
            var eid = $(this).attr("eid")
            var pageNum = $("#pageNo").val()
            deleteComment(eid, pageNum)
        })

        //发表评论
        function addEvaluate() {
            $.post("evaluate/addEvaluate",$("#evaluateEdit").serialize(),function (result) {
                if(result.status == 1){
                    Dreamer.success(result.message)
                    //重新查询评论
                    getEvaluate(bid,1)
                    document.getElementById("evaluateEdit").reset()
                }else{
                    Dreamer.error(result.message)
                }
            })
        }

        //删除评论
        function deleteComment(eid, pageNum) {
            <%--onclick="deleteComment(${evaluate.eid},${result.pageNum})"--%>
            // alert(eid+"---"+pageNum)
            if (confirm("你确定要删除该评论吗?")) {
                $.post("evaluate/deleteComment", "eid=" + eid, function (result) {
                    if (result.status == 1) {
                        //删除成功
                        Dreamer.success(result.message);
                        //删除数据是最后一页的最后一条数据时，做个标记，当删除成功时，返回前一页
                        if ($("#flag").val() == "true") {
                            //刷新
                            getEvaluate(bid, (pageNum - 1))
                        } else {
                            //刷新
                            getEvaluate(bid, pageNum)
                        }
                    } else {
                        Dreamer.error(result.message);
                    }
                });
            }

        }

        //查博客详情
        function getBlogDetail(bid) {
            $.post("blog/getBlogUserByid", "bid=" + bid, function (result) {
                // alert(JSON.stringify(result))
                $("#uname").html(result.user.truename);
                $("#email").html(result.user.email);
                $("#content").html(result.bcontent);
                $("#time").html(result.date);
                //查评价
                getEvaluate(result.bid, 1)
            });
        }

        //查博客详情下的评论
        function getEvaluate(bid, pageNo) {
            $("#pageNo").val(pageNo)
            $.post("evaluate/getEvaluate", {"bid": bid, "pageNo": pageNo}, function (result) {
                if(result.total != 0){
                    //有评论
                    //删除数据是最后一页的最后一条数据时，做个标记，当删除成功时，返回前一页
                    if (result.total % result.pageSize == 1 && result.pageNum == result.pages) {
                        $("#flag").val(true)
                    } else {
                        $("#flag").val(false)
                    }

                    // alert(JSON.stringify(result))
                    let evaluatelist = '';
                    result.list.forEach(function (evaluate) {
                        evaluatelist += `
                        <tr>
							<td>\${evaluate.econtent}</td>
							<td>\${evaluate.etime}</td>
							<td><button type="button" eid="\${evaluate.eid}" class="deletecomment" >删除</button></td>
						</tr>
                    `;
                    });
                    $("#evaluates").html(evaluatelist);
                    //处理分页
                    let start = '';
                    if (result.pageNum <= 1) {
                        start += `<a href="javascript:void(0)"><span style="color: #3a3c3c">首页</span></a>&nbsp;
                                <a href="javascript:void(0)"><span style="color: #3a3c3c">上一页</span></a>`;
                    } else {
                        start += `<a href="javascript:getEvaluate(\${bid},1)">首页</a>&nbsp;
                                <a href="javascript:getEvaluate(\${bid},\${result.pageNum-1})">上一页</a>`;
                    }
                    let end = '';
                    if (result.pageNum >= result.pages) {
                        end += `<a href="javascript:void(0)"><span style="color: #3a3c3c">下一页</span></a>&nbsp;
                                <a href="javascript:void(0)"><span style="color: #3a3c3c">末页</span></a>`;
                    } else {
                        end += `<a href="javascript:getEvaluate(\${bid},\${result.pageNum+1})">下一页</a>&nbsp;
                                <a href="javascript:getEvaluate(\${bid},\${result.pages})">末页</a>`;
                    }

                    let nums = '';
                    result.navigatepageNums.forEach(function (num) {
                        if (num == result.pageNum) {
                            nums += `<a href="javascript:void(0)" style="color: red">\${num}</a>&nbsp;&nbsp;&nbsp;`;
                        } else {
                            nums += `<a href="javascript:getEvaluate(\${bid},\${num})">\${num}</a>&nbsp;&nbsp;&nbsp;`;
                        }
                    })

                    let foot = `\${start}  【&nbsp;&nbsp;\${nums}】  \${end}   |||   总数据量:\${result.total}`;
                    $("#pagenationId").html(foot)
                }else{
                    //没有评论
                    // let img = ``
                    // $("#evaluates").html(img);
                }

            })
        }
    </script>
</head>
<body>

<input type="hidden" name="pageNo" id="pageNo">
<input type="hidden" name="flag" id="flag">
<h3><strong>博客信息：</strong></h3>
<table class="table table-condensed">
    <tr>
        <td width="18%" height="30" align="center">博主名称：</td>
        <td id="uname" width="82%" class="word_grey">
            <%--博主名称--%>
        </td>
    </tr>
    <tr>
        <td height="28" align="center">邮箱</td>
        <td height="28" id="email"></td>
    </tr>
    <tr>
        <td height="28" align="center">发布内容：</td>
        <td height="28" id="content"></td>
    </tr>
    <tr>
        <td height="28" align="center">发布时间：</td>
        <td height="28" id="time"></td>
    </tr>
</table>

<h3><strong>全部评价内容：</strong></h3>
<table class="table table-bordered">
    <thead>
    <tr>
        <th>评价内容</th>
        <th>评价时间</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="evaluates">
    <%--追加评论信息--%>

    </tbody>
    <tfoot id="tfoot">
    <%--设置分页信息--%>
    <tr>
        <td colspan="5" id="pagenationId">
            <center><img src="images/evaluate.jpg"  alt=""></center>
        </td>
    </tr>
    </tfoot>
</table>
<form id="evaluateEdit" action="JavaScript:void(0)">
    <input type="hidden" id="bFk" name="bFk">
    <input type="text" name="econtent" placeholder="善于结善缘，恶语伤人心~" >
    <button onclick="addEvaluate()">发表</button>
</form>
<button><a href="javascript:location.reload()">刷新</a></button>
</body>

</html>
